const parser = require("@babel/parser");
const template = require("@babel/template").default;
const traverse = require("@babel/traverse").default;
const type = require("@babel/types");
const generator = require("@babel/generator").default;
const path = require('path');
const fs = require('fs');

const visitor={
    VariableDeclarator(path){
        var node =path.node;//获取节点
        if(!type.isObjectExpression(node.init)) //判断node的初始化节点是否为ObjectExpression
            return;
        var objPropertiesList = node.init.properties; //获取到当前节点初始化的对象
        if (objPropertiesList.length==0) //当前节点的对象不为空
            return;
        var objName = node.id.name;//获取当前节点的名称
        objPropertiesList.forEach(prop=>{
            var key=prop.key.value;
            if (!type.isStringLiteral(prop.value))
            {
            var retStmt = prop.value.body.body[0];
            var fnPath = path.getFunctionParent();
            fnPath.traverse({
                CallExpression:function (_path) {
                    if (!type.isMemberExpression(_path.node.callee))
                        return;
                    var _node=_path.node.callee;
                    if(!type.isIdentifier(_node.object)||_node.object.name !==objName)
                        return;
                    if(!type.isStringLiteral(_node.prototype) || _node.property.value !=key)
                        return;
                    var args = _path.node.arguments;
                    //二元运算
                    if (type.isbinaryExpression(retStmt.argument) &&args.length===2)
                    {
                        _path.replaceWith(type.binaryExpression(retStmt.argument.operator,args[0],args[1]))
                    }
                    //逻辑运算
                    else if(type.isLogicalExpression(retStmt.argument) && args.length**2)
                    {
                       _path.replaceWith(type.logicalExpression(retStmt.argument.operator,args[0],args[1]))
                    }
                    //函数调用
                    else  if(type.isCallExpression(retStmt.argument) && type.isIdentifier(retStmt.argument.callee))
                    {
                        _path.replaceWith(type.callExpression(args[0], args.slice(1)))
                    }
                }
            })
            }
            else{
                var retStmt=prop.value.value //获取到objPropertiesList的下一步不是StringLiteral的值
                var fnPath = path.getFunctionParent();
                fnPath.traverse({
                    MemberExpression:function (_path) {
                        var _node = _path.node;
                        if (!type.isIdentifier(_node.object) || _node.object.name !== objName)
                             return; //当前节点属性为Identifier(调用节点)且调用的名称符合
                        if (!type.isStringLiteral(_node.property)||_node.property.value !=key)
                            return; //当前节点属性为StringLiteral(字符串) 且字符串名称等于当前节点
                        _path.replaceWith(type.stringLiteral(retStmt)) //替换值
                    }
                })
            }
        })
    path.remove(); //删除当前所匹配节点
    },
    ExpressionStatement(path){
        var node=path.node;
        if (!type.isCallExpression(node.expression))
            return;
        if (node.expression.arguments==undefined || node.expression.callee.params==undefined)
            return;
        var argumentList=node.expression.arguments;
        var paramList=node.expression.callee.params;
        for (var i =0;i<argumentList.length;i++)
        {
            var argumentName = argumentList[i].name;
            var paramName = paramList[i].name;

            path.traverse({
            MemberExpression:function (_path) {
                var _node = _path.node;
                if (!type.isIdentifier(_node.object) || _node.object.name !== paramName)
                    return;

                _node.object.name = argumentName;
                }
            });
        }
        node.expression.arguments = [];
        node.expression.callee.params = [];
    }

}
fs.readFile(path.resolve(__dirname, './source2.js'), { "encoding": 'utf-8' }, function (err, data) {
    const ast = parser.parse(data);
    traverse(ast, visitor);
    // 将ast 转为 js
    let { code } = generator(ast);
    // code = code.replace(/!!\[\]/g, 'true').replace(/!\[\]/g, 'false');
    console.log(code);
});